/*
 * Copyright (c) 2017,安徽皖通科技股份有限公司 All rights reserved.
 */

package com.wttech.dzzf.process.service.base;

import cn.com.taiji.common.manager.net.http.binclient.ApiRequestException;
import cn.com.taiji.sdk.comm.ETCCommHelper;
import cn.com.taiji.sdk.model.comm.protocol.AbstractSdtsRequest;
import cn.com.taiji.sdk.model.comm.protocol.AbstractSdtsResponse;
import com.wttech.dzzf.process.constant.UserConstants;
import com.wttech.dzzf.process.dao.DataInitilizingDao;
import com.wttech.dzzf.process.dao.ImportHisDataDao;
import com.wttech.dzzf.process.dao.TransferUserInfoDao;
import com.wttech.dzzf.process.model.FundTemp;
import com.wttech.dzzf.process.model.Organization;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import static com.wttech.dzzf.process.constant.UserConstants.RIGHT;

/**
 * 基本信息处理服务实现
 *
 * @author MaShibo
 * @date 2017/11/16.
 */
@Service
public class BaseInfoService {

	private static final Logger LOGGER = LoggerFactory.getLogger(BaseInfoService.class);

	private static final int ACCOUNTNO_LENGTH = 10;

	@Resource
	private TransferUserInfoDao transferUserInfoDao;

	@Resource
	private ImportHisDataDao importHisDataDao;

	@Resource
	private DataInitilizingDao dataInitilizingDao;


	/**
	 * 获取网点编号对应的渠道ID
	 *
	 * @param clientNo DZZF网点编号
	 * @return 网点编号对应的渠道编号
	 */
	public String obtainChannelId(String clientNo) {
		String channelId = UserConstants.DEFAULT_CHANNELID;
		Organization org = transferUserInfoDao.queryOrganizationByNo(clientNo);
		if (org == null || StringUtils.isBlank(org.getChannelId())) {
			LOGGER.info("网点编号{}无对应的渠道ID", clientNo);
			return channelId;
		} else {
			channelId = org.getChannelId();
		}
		return channelId;
	}


	/**
	 * 获取序列号
	 *
	 * @param transTime 交易时间
	 * @param seqNoMap  序列号MAP
	 * @return 序列号
	 */
	public synchronized Integer getExserialNum(String transTime, Map<String, Integer> seqNoMap) {
		Integer seqNo;
		if (seqNoMap.containsKey(transTime)) {
			seqNo = seqNoMap.get(transTime);
			seqNoMap.put(transTime, seqNo + 1);
		} else {
			seqNo = 1;
			seqNoMap.put(transTime, 2);
		}
		return seqNo;
	}

	/**
	 * 获取future结果
	 *
	 * @param futures future集合
	 * @param <T>     多线程执行结果
	 */
	public <T> void getFutureResult(List<Future<T>> futures) {
		futures.forEach(future -> {
			try {
				future.get();
			} catch (InterruptedException e) {
				Thread.currentThread().interrupt();
				LOGGER.error(e.getMessage(), e);
			} catch (ExecutionException e) {
				LOGGER.error(e.getMessage(), e);
			}
		});
	}

	public String generateAccountNo(Integer accountType) {
		String sequenceName = "SEQUENCE" + accountType + "ACCOUNTNO";
		Long seqNo = importHisDataDao.querySequenceNo(sequenceName);
		StringBuilder seqNoStr = new StringBuilder(seqNo.toString());
		while (seqNoStr.length() < ACCOUNTNO_LENGTH) {
			seqNoStr.insert(0, "0");
		}
		seqNoStr.insert(0, "20171223").insert(0, accountType.toString());
		return seqNoStr.toString();
	}

	public void createFailtureId(String fundAccountId) {
		try {
			FundTemp temp = new FundTemp();
			temp.setFundAccountId(fundAccountId);
			transferUserInfoDao.createTestObjcet(temp);
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
		}
	}

	public String buildMonitorMsg(String[] tables) {
		if (tables == null || tables.length < 1) {
			return null;
		}
		StringBuilder msg = new StringBuilder("\n");
		Arrays.stream(tables).forEach(tableName -> {
			try {
				Integer num = dataInitilizingDao.queryTableNum(tableName);
				msg.append(tableName).append(" 中已创建").append("【").append(num).append("】").append("条数据\n");
			} catch (Exception e) {
				LOGGER.error(e.getMessage(), e);
			}
		});
		msg.deleteCharAt(msg.length() - 1);
		return msg.toString();
	}

	public boolean needOutput(String result) {
		return !StringUtils.equalsIgnoreCase(result, RIGHT);
	}



}
